package com.evil.enterprise.controller;

import com.evil.application.api.RemoteApplicationFormControlService;
import com.evil.application.api.RemoteApplicationService;
import com.evil.application.pojo.dto.form.ApplicationFormControlRespDTO;
import com.evil.application.pojo.dto.form.FormControlListReqDTO;
import com.evil.application.pojo.dto.form.FormControlTreeRespDTO;
import com.evil.application.pojo.dto.form.ModifyApplicationFormControlReqDTO;
import com.evil.application.pojo.dto.form.control.*;
import com.evil.application.pojo.dto.template.print.QueryPrintTemplateConfigReqDTO;
import com.evil.common.account.dto.department.DepartmentBaseRespDTO;
import com.evil.common.account.dto.user.UserBaseRespDTO;
import com.evil.common.application.dto.ApplicationKeyVersionReqDTO;
import com.evil.common.core.entity.R;
import com.evil.common.log.annotation.RequestLogAnnotation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 应用表单接口
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/application/form/control")
public class ApplicationFormControlController {

    private final RemoteApplicationService remoteApplicationService;

    private final RemoteApplicationFormControlService remoteApplicationFormControlService;

    /**
     * 应用表单控件信息
     *
     * @param applicationFormControlInfoReqTO applicationFormControlInfoReqTO
     * @return R
     */
    @PostMapping("/info")
    public R<ApplicationFormControlRespDTO> applicationFormControlInfo(@Validated @RequestBody ApplicationFormControlInfoReqTO applicationFormControlInfoReqTO) {
        return R.success(remoteApplicationFormControlService.applicationFormControlInfo(applicationFormControlInfoReqTO));
    }

    /**
     * 应用表单控件基础信息
     *
     * @param applicationKeyVersionReqDTO applicationKeyVersionReqDTO
     * @return R
     */
    @PostMapping("/base-info")
    public R<List<ControlBaseInfoDTO>> applicationFormControlBaseInfo(@Validated @RequestBody ApplicationKeyVersionReqDTO applicationKeyVersionReqDTO) {
        return R.success(remoteApplicationFormControlService.applicationFormControlBaseInfo(applicationKeyVersionReqDTO));
    }

    /**
     * 应用表单控件列表
     *
     * @param formControlListReqDTO formControlListReqDTO
     * @return R
     */
    @PostMapping("/list")
    public R<List<ControlBaseInfoDTO>> applicationFormControlList(@Validated @RequestBody FormControlListReqDTO formControlListReqDTO) {
        return R.success(remoteApplicationFormControlService.applicationFormControlList(formControlListReqDTO));
    }

    /**
     * 编辑应用表单控件
     *
     * @param modifyApplicationFormControlReqDTO modifyApplicationFormControlReqDTO
     * @return R
     */
    @RequestLogAnnotation
    @PostMapping("/modify")
    public R<?> modifyApplicationFormControl(@Validated @RequestBody ModifyApplicationFormControlReqDTO modifyApplicationFormControlReqDTO) {
        remoteApplicationFormControlService.modifyApplicationFormControl(modifyApplicationFormControlReqDTO);
        return R.success();
    }

    /**
     * 编辑应用表单控件并发布
     *
     * @param modifyApplicationFormControlReqDTO modifyApplicationFormControlReqDTO
     * @return R
     */
    @RequestLogAnnotation
    @PostMapping("/modify-publish")
    public R<?> modifyAndPublishApplicationFormControl(@Validated @RequestBody ModifyApplicationFormControlReqDTO modifyApplicationFormControlReqDTO) {
        // 两次操作 独立事务
        remoteApplicationFormControlService.modifyApplicationFormControl(modifyApplicationFormControlReqDTO);
        remoteApplicationService.publishApplication(modifyApplicationFormControlReqDTO.getApplicationKey());
        return R.success();
    }

    /**
     * 应用表单控件条件
     *
     * @param formControlListReqDTO formControlListReqDTO
     * @return R
     */
    @PostMapping("/condition")
    public R<List<ControlConditionRespDTO>> applicationFormControlCondition(@Validated @RequestBody FormControlListReqDTO formControlListReqDTO) {
        return R.success(remoteApplicationFormControlService.applicationFormControlCondition(formControlListReqDTO));
    }

    /**
     * 应用表单控件树
     *
     * @return R
     */
    @PostMapping("/tree")
    public R<List<FormControlTreeRespDTO>> applicationFormControlTree(@Validated @RequestBody ControlTreeReqDTO controlTreeReqDTO) {
        return R.success(remoteApplicationFormControlService.applicationControlTree(controlTreeReqDTO));
    }

    /**
     * 应用表单控件 可选择部门列表
     *
     * @param memberSelControlReqDTO memberSelControlReqDTO
     * @return R
     */
    @PostMapping("/dept-list")
    public R<List<DepartmentBaseRespDTO>> getDeptSelectList(@Validated @RequestBody MemberSelControlReqDTO memberSelControlReqDTO) {
        return R.success(remoteApplicationFormControlService.getDeptSelectList(memberSelControlReqDTO));
    }

    /**
     * 应用表单控件 可选择成员列表
     *
     * @param memberSelControlReqDTO memberSelControlReqDTO
     * @return R
     */
    @PostMapping("/user-list")
    public R<List<UserBaseRespDTO>> getUserSelectList(@Validated @RequestBody MemberSelControlReqDTO memberSelControlReqDTO) {
        return R.success(remoteApplicationFormControlService.getUserSelectList(memberSelControlReqDTO));
    }

    /**
     * 查看打印模板字段对照配置
     *
     * @param queryPrintTemplateConfigReqDTO queryTemplateConfigReqDTO
     * @return R
     */
    @PostMapping("/template-config")
    public R<?> queryPrintTemplateConfig(@Validated @RequestBody QueryPrintTemplateConfigReqDTO queryPrintTemplateConfigReqDTO) {
        return R.success(remoteApplicationFormControlService.queryPrintTemplateConfig(queryPrintTemplateConfigReqDTO));
    }
}
